package com.luocj.looook.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.luocj.looook.R;
import com.luocj.looook.URL;
import com.luocj.looook.activity.WebViewActivity;
import com.luocj.looook.adapter.BaseRecyclerAdapter;
import com.luocj.looook.adapter.SmartViewHolder;
import com.luocj.looook.base.BaseFragment;
import com.luocj.looook.model.NbaModel;
import com.luocj.looook.utils.Constant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luocj515 on 2017/11/21.
 * 项目名称：LooooK
 * 类描述：
 * 创建人：hasee
 * 创建时间：2017/11/21 14:24
 * 修改备注：
 */

public class NBAFragment extends BaseFragment {
    private static final String TAG = NBAFragment.class.getSimpleName();
    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView recyclerView;

    private List<NbaModel.NewslistBean> nbaListBean = new ArrayList<>();
    private boolean isFirstIn = true;
    private int PAGE_SIZE = 10;
    private BaseRecyclerAdapter<NbaModel.NewslistBean> nbaAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_nba;
    }

    @Override
    protected void initView() {
        Log.i(TAG, "initView: ");
        smartRefreshLayout = mContentView.findViewById(R.id.smartrefreshlayout_nba);
        recyclerView = mContentView.findViewById(R.id.recyclerview_nba);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContentView.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        nbaAdapter = new BaseRecyclerAdapter<NbaModel.NewslistBean>(nbaListBean, R.layout.item_nba, null) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, final NbaModel.NewslistBean model, final int position) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContentView.getContext(), WebViewActivity.class);
                        intent.putExtra(WebViewActivity.DATA_URL, model.getUrl());
                        startActivity(intent);
                    }
                });

                ImageView ivNba = holder.itemView.findViewById(R.id.iv_nba_pic);
                Glide.with(mContentView.getContext())
                        .load(model.getPicUrl())
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher_round)
                        .centerCrop()
                        .into(ivNba);

                holder.text(R.id.tv_nba_content, model.getTitle());
                holder.text(R.id.tv_nba_title, model.getDescription());
                holder.text(R.id.tv_nba_time, model.getCtime());

            }
        };

        recyclerView.setAdapter(nbaAdapter);
        if (isFirstIn) {
            isFirstIn = false;
            smartRefreshLayout.autoRefresh();
        }

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "run: " + "refresh");
                        refresh(true);
                    }
                }, 2000);
            }
        });

        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "run: " + "load more");
//                        loadMore();
                        refresh(false);
                    }
                }, 2000);
            }
        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mContentView != null) {
            return mContentView;
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initData() {
        Log.i(TAG, "initData: ");
//        refresh();
    }

    private void refresh(final boolean isRefresh) {

        if (isRefresh) {
            if (nbaListBean.size() != 0) {
                nbaListBean.clear();
            }
            PAGE_SIZE = 10;
        } else {
            PAGE_SIZE = PAGE_SIZE + 10;
        }
        OkGo.<String>get(URL.BASE_URL_NBA)
                .tag(TAG)
                .params("key", "861a794733dd2b057e415d822bef9586")
                .params("num", PAGE_SIZE)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        NbaModel nbaModel = JSONObject.parseObject(response.body().toString(), NbaModel.class);
                        Log.i(TAG, "onSuccess: " + nbaModel.getNewslist().size());
                        if (isRefresh) {
                            nbaAdapter.refresh(nbaModel.getNewslist());
                            smartRefreshLayout.finishRefresh();
                            smartRefreshLayout.setLoadmoreFinished(false);
                        } else {
                            nbaAdapter.loadmore(nbaModel.getNewslist().subList(nbaAdapter.getItemCount(), nbaModel.getNewslist().size()));
                            smartRefreshLayout.finishLoadmore();

                            if (nbaAdapter.getItemCount() >= Constant.MAX_SIZE) {
                                smartRefreshLayout.setLoadmoreFinished(true);
                            }
                        }
                        Log.i(TAG, "onSuccess: " + nbaAdapter.getItemCount());

                    }

                    @Override
                    public void onError(Response<String> response) {
                        Log.i(TAG, "onError: " + response.toString());
                    }
                });
    }
}
